<?php

namespace App\Admin\Controllers\School;

use App\Models\School\Classes;

use App\Models\School\Honor;
use App\Models\School\Report;
use App\Models\School\Student;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Request;

class HonorController extends Controller
{
    use ModelForm;

    public $header = '光荣榜列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Honor::class, function (Grid $grid) {
            $grid->model()->orderBy('honor_id','desc');
            $grid->honor_id('编号')->sortable();
            $grid->column('发布用户')->display(function (){
                return '平台';
            });
            $grid->class_id('班级id');
            $grid->class_name('班级');
            $grid->column('统计时间')->display(function (){
                return $this->start_time . '——' . $this->end_time;
            });

            $grid->create_at('发布时间');
            $grid->actions(function (Grid\Displayers\Actions $actions){
               $actions->disableEdit();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->between('create_at', '发布时间')->date();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Honor::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->row($this->detail($id));
    }

    public function detail($id)
    {
        $honor = Honor::where('honor_id',$id)->with('subjects')->first()->toArray();

        if (!$honor){
            abort(404);
        }

        /*$start_time = $honor['start_time'];
        $end_time   = $honor['end_time'];
        $subject    = pos($honor['subjects']);

        $class_subject_id = 0;
        if ($subject){
            $class_subject_id = $subject['class_subject_id'];
        }


        $data = [];

        if ($class_subject_id){
            $personal_report_data = Report::with('user')
                ->where('class_subject_id', $class_subject_id)
                ->where('class_id', $honor['class_id'])
                ->where('status',1)
                ->where('create_at','>=',$start_time . ' 00:00:00')
                ->where('create_at','<=',$end_time . ' 23:59:59')
                ->where('class_report_type','<>', 4)
                ->select(['student_id',DB::Raw('SUM(CASE WHEN behavior_type=1 THEN score ELSE score * (-1) END) AS score')])
                ->groupBy('student_id')
                ->orderBy('score','desc')
                ->get()->toArray();

            $group_report_data = Report::with('group')
                ->where('class_subject_id', $class_subject_id)
                ->where('class_id', $honor['class_id'])
                ->where('class_group_id', '>', 0)
                ->where('status', 1)
                ->where('create_at','>=', $start_time . ' 00:00:00')
                ->where('create_at','<=', $end_time . ' 23:59:59')
                ->where('class_report_type','<>', 4)
                ->select(['class_group_id',DB::Raw('SUM(CASE WHEN behavior_type=1 THEN score ELSE score * (-1) END) AS score')])
                ->groupBy('class_group_id')
                ->orderBy('score', 'desc')
                ->get()->toArray();

            $student_ids = array_column($personal_report_data, 'student_id');

            $student_data = Student::where('status', 1)
                ->where('class_id', $honor['class_id'])
                ->where('status',1)
                ->whereNotIn('student_id', $student_ids)
                ->select(['student_id','name_class'])
                ->get()->toArray();

            $data['personal_report_data'] = $personal_report_data;
            $data['group_report_data'] = $group_report_data;
            $data['student_data'] = $student_data;

        } else {
            abort(404);
        }*/

        return view('admin.school.honor-search',compact('honor'));
    }

    public function getHonorData(Request $request)
    {
        $data = [];
        $honor_id = $request->post('honor_id', 0);
        $class_subject_id = $request->post('class_subject_id', 0);

        if ($honor_id && $class_subject_id){
            $honor = Honor::where('honor_id',$honor_id)->first()->toArray();

            if ($honor) {
                $start_time = $honor['start_time'];
                $end_time   = $honor['end_time'];

                $personal_report_data = Report::with('user')
                    ->where('class_subject_id', $class_subject_id)
                    ->where('class_id', $honor['class_id'])
                    ->where('status',1)
                    ->where('create_at','>=',$start_time . ' 00:00:00')
                    ->where('create_at','<=',$end_time . ' 23:59:59')
                    ->where('class_report_type','<>', 4)
                    ->select(['student_id',DB::Raw('SUM(CASE WHEN behavior_type=1 THEN score ELSE score * (-1) END) AS score')])
                    ->groupBy('student_id')
                    ->orderBy('score','desc')
                    ->get()->toArray();

                $group_report_data = Report::with('group')
                    ->where('class_subject_id', $class_subject_id)
                    ->where('class_id', $honor['class_id'])
                    ->where('class_group_id', '>', 0)
                    ->where('status', 1)
                    ->where('create_at','>=', $start_time . ' 00:00:00')
                    ->where('create_at','<=', $end_time . ' 23:59:59')
                    ->where('class_report_type','<>', 4)
                    ->select(['class_group_id',DB::Raw('SUM(CASE WHEN behavior_type=1 THEN score ELSE score * (-1) END) AS score')])
                    ->groupBy('class_group_id')
                    ->orderBy('score', 'desc')
                    ->get()->toArray();

                $student_ids = array_column($personal_report_data, 'student_id');

                $student_data = Student::where('status', 1)
                    ->where('class_id', $honor['class_id'])
                    ->where('status',1)
                    ->whereNotIn('student_id', $student_ids)
                    ->select(['student_id','name_class'])
                    ->get()->toArray();

                $data['personal_report_data'] = $personal_report_data;
                $data['group_report_data'] = $group_report_data;
                $data['student_data'] = $student_data;
            }

        }

        return view('admin.school.honor-template',compact('data'));
    }
}
